﻿Public Class frmpedido
    Dim rs As ADODB.Recordset
    Dim sql As String
    Dim precio(5) As Integer
    Dim i As Integer = 0
    Dim total As Integer = 0
    Dim l As Integer = 0
    Dim w As Integer = 0
    Private Sub frmpedido_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        conectar()
        sql = "SELECT DESCRIPCION FROM PLATOS"
        rs = coneccion.Execute(sql)
        While rs.EOF <> True
            If rs.EOF = True Then
                Exit While
            End If
            cboplatos.Items.Add(rs.Fields(0).Value)
            rs.MoveNext()
        End While
        sql = "SELECT DESCRIPCION FROM TIPO_PLATO"
        rs = coneccion.Execute(sql)
        While rs.EOF <> True  'ES UN CONDICIONAL BOLEANO EN DONDE ES FALSO CUANDO CONTIENE DATOS Y SI ES TRUE ESTA VACIO 
            If rs.EOF = True Then
                Exit While
            End If
            cmbTipo.Items.Add(rs.Fields(0).Value)
            rs.MoveNext()
        End While

    End Sub
    Private Sub cboplatos_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboplatos.SelectedIndexChanged
        btnagregar.Enabled = True
        txtcantidad.Focus()

    End Sub

    Private Sub btnagregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnagregar.Click

        sql = "SELECT PLATOS.DESCRIPCION, TIPO_PLATO.DESCRIPCION, PLATOS.COSTO FROM (TIPO_PLATO INNER JOIN  PLATOS ON TIPO_PLATO.CODI_TIPO = PLATOS.CODI_TIPO) WHERE (TIPO_PLATO.DESCRIPCION = '" & cmbTipo.SelectedItem.ToString & "') AND (PLATOS.DESCRIPCION = '" & cboplatos.SelectedItem.ToString & "')"
        rs = coneccion.Execute(sql)
        If rs.EOF <> True Then
            rs.MoveFirst()
            txtprecio.Text = rs.Fields(2).Value.ToString
        End If
        btnagregar.Enabled = False
        If lsbplatillo.Items.Count = i Then
            lsbplatillo.Items.Add(cboplatos.SelectedItem.ToString)
            lsbprecio.Items.Add(txtprecio.Text)
            lsbcantidad.Items.Add(txtcantidad.Text)
            i = i + 1
        End If
        lsbplatillo.Enabled = True : lsbprecio.Enabled = True
        lsbcantidad.Enabled = True
    End Sub
    Private Sub txtcantidad_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtcantidad.Validated
        If txtcantidad.Text = "" Then
            txtcantidad.Focus()
        End If
    End Sub
    Private Sub txtcantidad_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtcantidad.TextChanged
        If txtcantidad.Text = "" Then
            cboplatos.Enabled = False
        Else
            cboplatos.Enabled = True
            Dim indice As Integer
            For indice = 0 To lsbprecio.Items.Count - 1
                w = w + Val(lsbprecio.Items.Item(indice)) * Val(lsbcantidad.Items.Item(indice))
                total = total + w
                w = 0
            Next
            txtTotal.Text = total
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImprimir.Click
        Dim totales As Double = 0
        For a As Integer = 0 To lsbprecio.Items.Count - 1
            totales = totales + lsbcantidad.Items.Item(a) * lsbprecio.Items.Item(a)
        Next
        txtTotal.Text = totales
        Console.WriteLine(totales)
        Me.Visible = False

    End Sub

    Private Sub btnVerificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerificar.Click
        Dim mostrar As New Resultados(txtPaterno.Text, txtMaterno.Text, txtNombre.Text, txtCodigo.Text)
        mostrar.Show()
    End Sub
    Public Sub Enviar(ByVal Resultado As String)
        sql = "SELECT CODI_CLIEN, NOMB_CLIEN, PATE_CLIEN, MATE_CLIEN FROM(CLIENTE) WHERE (CODI_CLIEN = '" & Resultado & "')"
        rs = coneccion.Execute(sql)
        If rs.EOF <> True Then
            rs.MoveFirst()
            txtCodigo.Text = rs.Fields(0).Value.ToString
            txtNombre.Text = rs.Fields(1).Value.ToString
            txtPaterno.Text = rs.Fields(2).Value.ToString
            txtMaterno.Text = rs.Fields(3).Value.ToString

        End If
    End Sub

    Private Sub cmbTipo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbTipo.SelectedIndexChanged
        Enviar(Resultado)
        btnagregar.Enabled = True
        cboplatos.Items.Clear()
        sql = "SELECT PLATOS.DESCRIPCION, TIPO_PLATO.DESCRIPCION FROM (PLATOS INNER JOIN TIPO_PLATO ON PLATOS.CODI_TIPO = TIPO_PLATO.CODI_TIPO) WHERE (TIPO_PLATO.DESCRIPCION = '" & cmbTipo.SelectedItem.ToString & "')"
        rs = coneccion.Execute(sql)
        While rs.EOF <> True
            If rs.EOF = True Then
                Exit While
            End If
            cboplatos.Items.Add(rs.Fields(0).Value)
            rs.MoveNext()
        End While
    End Sub
End Class